package com.shaper2.minaapps;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: danlek
 * Date: 2012-11-02
 * Time: 09:46
 * To change this template use File | Settings | File Templates.
 */
public class MySQLiteDatabaseHandler extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "shapes";
    private static final String TABLE_NAME = "coordinates";

    private static final String KEY_ID = "id";
    private static final String KEY_LAT = "lat";
    private static final String KEY_LON = "lon";

    public MySQLiteDatabaseHandler(Context context){
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        String CREATE_SHAPE_TABLE = "CREATE TABLE " + TABLE_NAME + " ("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_LAT + " INTEGER,"
                + KEY_LON + " INTEGER" + ")";
        database.execSQL(CREATE_SHAPE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, int i, int i1) {
        database.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(database);
    }

    //CRUD
    public void addCoordinate(MyCoordinates coordinates){
        SQLiteDatabase database = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_LAT, coordinates.getLat());
        values.put(KEY_LON, coordinates.getLon());
        database.insert(TABLE_NAME, null, values);
        database.close();
    }

    //Get single
    public MyCoordinates getCoordinate(int id){
        SQLiteDatabase database = this.getReadableDatabase();
        Cursor cursor = database.query(TABLE_NAME, new String[] { KEY_ID, KEY_LAT, KEY_LON},
                KEY_ID + "=?", new String[] {String.valueOf(id)}, null, null, null, null);
        if(cursor != null)
            cursor.moveToFirst();
        MyCoordinates coordinates = new MyCoordinates(Integer.parseInt(cursor.getString(0)),
                Integer.parseInt(cursor.getString(1)), Integer.parseInt(cursor.getString(2)));
        database.close();
        return coordinates;
    }

    //Get ALL
    public List<MyCoordinates> getAllCoordinates(){
        List<MyCoordinates> coordinatesList = new ArrayList<MyCoordinates>();
        String selectQuery = "SELECT * FROM " + TABLE_NAME;
        SQLiteDatabase database = getReadableDatabase();
        Cursor cursor = database.rawQuery(selectQuery, null);

        if(cursor.moveToFirst()){
            do {
                MyCoordinates coordinates = new MyCoordinates();
                coordinates.setID(Integer.parseInt(cursor.getString(0)));
                coordinates.setLat(Integer.parseInt(cursor.getString(1)));
                coordinates.setLon(Integer.parseInt(cursor.getString(2)));
                coordinatesList.add(coordinates);

            } while(cursor.moveToNext());
        }
        database.close();
        return coordinatesList;
    }
}
